<?php
// +----------------------------------------------------------------------
// | 幕小铺
// +----------------------------------------------------------------------
// | 版权所有 2016~2019 幕小铺科技（上海）有限公司 [ http://www.XXX.com ]
// +----------------------------------------------------------------------
// | 2020/03/14 jason wang
// +----------------------------------------------------------------------
namespace app\erp\logic;

use think\facade\Hook;
use think\DB;
use util\Tree;
use think\facade\Debug;
use think\facade\Cache;
/**
 * 储值管理
 * @package app\erp\model
 */
class Balancelogic{

    /**
     * 首页
     * @author  dou<51779987@qq.com>
     * @return array_out(code, message, data)
     */
    public function rules_list($user_id){
        DB::startTrans();//启动事务
        try{
            /********行为参数赋值（*）********/
            $params = [
				'user_id' => $user_id,
            ];
            /********行为参数赋值（*）********/
     
            /********前置行为********/
            $hook_rel = logic_hook($action = __FUNCTION__, $params, 'before');
            if($hook_rel['code'] < 0){
                DB::rollback();
                return array_out(-1002, $hook_rel['message']);//前置错误 -1002
            }
            /********前置行为********/

            /********业务逻辑********/
            $map = [];//搜索处理
            /*if($user_id != '') $map[] = ['uid', '=', $user_id];*/
            $map[] = ['status', '>', 0];
            $list = model('user_center/UcMemberBalanceRules', 'model\user')->where($map) -> order('man_price asc')->paginate();//获取列表
            $rel_list = [];
            foreach($list as $key => $value){
				$rel_list[$key]['id'] 			    = (int)$value['id'];
				$rel_list[$key]['name'] 		    = (string)$value['name'];
				$rel_list[$key]['man_price'] 	    = (string)(int)$value['man_price'];
				$rel_list[$key]['song_price']       = (string)(int)$value['song_price'];
				$rel_list[$key]['level'] 		    = (int)$value['level'];
				$rel_list[$key]['create_time'] 		= (int)$value['create_time'];
				$rel_list[$key]['update_time'] 	    = (int)$value['update_time'];
				$rel_list[$key]['status'] 	        = (int)$value['status'];
				$rel_list[$key]['superpose'] 	    = (int)$value['superpose'];
			}
            $result_list = [];
            $result_list['total']  			= (int)$list -> total();//总数
            $result_list['current_page']	= (int)$list -> currentPage();//页码
            $result_list['last_page']   	= (int)$list -> lastPage();//总页数
            $result_list['per_page']   		= (int)$list -> listRows();//每页条数
            $result_list['data'] 			= $rel_list;
            /********业务逻辑********/
			
            /********后置行为********/
            $hook_rel = logic_hook($action = __FUNCTION__, $params, 'after');
            if($hook_rel['code'] < 0){
                DB::rollback();
                return array_out(-1003, $hook_rel['message']);//后置错误 -1003
            }
            /********后置行为********/

            /********逻辑返回********/
			DB::commit();
            return array_out(200, 'success', $result_list);
            /********逻辑返回********/
			
        } catch (\Exception $e) {
			DB::rollback();
            return array_out(-9999, $e -> getMessage());//异常错误 -9999
        }
    }


    /**
     * 储值流水
     * @author  dou<51779987@qq.com>
     */
    public function balance_record($user_id,$journal_id)
    {
        DB::startTrans();//启动事务
        try{
            /********行为参数赋值（*）********/
            $params = [
                'user_id' => $user_id,
            ];
            /********行为参数赋值（*）********/

            /********前置行为********/
            $hook_rel = logic_hook($action = __FUNCTION__, $params, 'before');
            if($hook_rel['code'] < 0){
                DB::rollback();
                return array_out(-1002, $hook_rel['message']);//前置错误 -1002
            }
            /********前置行为********/

            /********业务逻辑********/
            $ine = [0=>'暂无',1=>'充值',2=>'赠送',3=>'消费'];
            $pay_type = [0=>'暂无',1=>'微信',2=>'支付宝',3=>'现金',4=>'余额'];
            $pay_status = [0=>'待支付',1=>'已支付',2=>'已完成',3=>'已取消',4=>'已过期', 5=>'待确认'];

            $map = [];//搜索处理
            $map['uid'] = $user_id;
            $rel_list = [];
            $list = model('user_center/UcMemberBalanceJournalLog', 'model\user')->where($map)->order('create_time desc') -> select();
            foreach ($list as $key => $value) {
                $rel_list[$key]['id'] 			        = (int)$value['id'];
                $rel_list[$key]['uid'] 			        = (int)$value['uid'];
                $rel_list[$key]['nick_name'] 			= (string)$value->get_uc_member->nick_name;
                $rel_list[$key]['create_time'] 			= (int)$value['create_time'];
                $rel_list[$key]['journal_id'] 			= (int)$value['journal_id'];
                $rel_list[$key]['ine'] 			        = (int)$value['ine'];
                $rel_list[$key]['ine_nick'] 			= $ine[(int)$value['ine']];
                if($value['ine'] == 3){
                    $rel_list[$key]['price'] 			    = (string)'-'.$value['price'];
                }else{
                    $rel_list[$key]['price'] 			    = (string)'+'.$value['price'];
                }

                $rel_list[$key]['use_rule'] 			= (string)$value['use_rule'];
                $rel_list[$key]['log_remark'] 			= (string)$value['log_remark'];
                $rel_list[$key]['balance_journal'] = [];

                if(isset($value -> get_uc_member_balance)){
                    $rel_list[$key]['balance_journal']['id'] 			        = (int) ($value -> get_uc_member_balance -> id);
                    $rel_list[$key]['balance_journal']['uid'] 			        = (int)$value -> get_uc_member_balance -> uid;
                    $rel_list[$key]['balance_journal']['nick_name'] 			= (string)$value->get_uc_member->nick_name;
                    $rel_list[$key]['balance_journal']['pay_price'] 		    = (string)$value->get_uc_member_balance->pay_price;
                    $rel_list[$key]['balance_journal']['pay_order_id'] 		    = (int)$value->get_uc_member_balance->pay_order_id;
                    $rel_list[$key]['balance_journal']['create_time'] 		    = (int)$value->get_uc_member_balance->create_time;
                    $rel_list[$key]['balance_journal']['update_time'] 		    = (int)$value->get_uc_member_balance->update_time;
                    $rel_list[$key]['balance_journal']['pay_type'] 			    = (int)$value->get_uc_member_balance->pay_type;
                    $rel_list[$key]['balance_journal']['pay_type_nick'] 	    = $pay_type[(int)$value->get_uc_member_balance->pay_type];
                    $rel_list[$key]['balance_journal']['pay_status_nick'] 	= $pay_status[(int)$value->get_uc_member_balance->pay_status];
                    $rel_list[$key]['balance_journal']['order_code'] 		    = (string)$value->get_uc_member_balance->order_code;
                    $rel_list[$key]['balance_journal']['pay_expiration_time']   = (int)$value->get_uc_member_balance->pay_expiration_time;
                    $rel_list[$key]['balance_journal']['payover_time'] 			= (int)$value->get_uc_member_balance->payover_time;
                }

                if($value['ine'] == 3 && isset($value -> get_erp_goods_order)){
                    $rel_list[$key]['balance_journal']['pay_status_nick'] 	    = '消费成功';//支付状态
                    $rel_list[$key]['balance_journal']['pay_type_nick'] 	    = $pay_type[$value -> get_erp_goods_order -> pay_type];//付款方式
                    $rel_list[$key]['balance_journal']['order_code'] 	        = $value -> get_erp_goods_order -> ordercode;//支付单号
                }
                if(empty($rel_list[$key]['balance_journal'])){
                    $rel_list[$key]['balance_journal'] = (object)[];
                }
            }

            $result_list = [];
           /* $result_list['total']  			= (int)$list['data']['info'] -> total();//总数
            $result_list['current_page']	= (int)$list['data']['info'] -> currentPage();//页码
            $result_list['last_page']   	= (int)$list['data']['info'] -> lastPage();//总页数
            $result_list['per_page']   		= (int)$list['data']['info'] -> listRows();//每页条数*/
            //$result_list['data'] 			= $rel_list;

            $page       = input('page');
            $page_size  = input('page_size');
            $count = count($rel_list);//总条数
            $start=($page-1)*$page_size;//偏移量，当前页-1乘以每页显示条数
            $result_list['data'] = array_slice($rel_list, $start, $page_size);
            /********业务逻辑********/

            /********后置行为********/
            $hook_rel = logic_hook($action = __FUNCTION__, $params, 'after');
            if($hook_rel['code'] < 0){
                DB::rollback();
                return array_out(-1003, $hook_rel['message']);//后置错误 -1003
            }
            /********后置行为********/

            /********逻辑返回********/
            DB::commit();
            return array_out(200, 'success', $result_list);
            /********逻辑返回********/

        } catch (\Exception $e) {
            DB::rollback();
            return array_out(-9999, $e -> getMessage());//异常错误 -9999
        }
    }

}